package com.facebook.presto.tdengine;

import com.facebook.presto.spi.ColumnHandle;
import com.facebook.presto.spi.ConnectorSession;
import com.facebook.presto.spi.ConnectorSplit;
import com.facebook.presto.spi.RecordSet;
import com.facebook.presto.spi.connector.ConnectorRecordSetProvider;
import com.facebook.presto.spi.connector.ConnectorTransactionHandle;
import com.google.common.collect.ImmutableList;

import java.util.List;


//该对象是实际读取数据源的对象，所有数据读的操作到这在里面。
public class TdEngineRecordSetProvider implements ConnectorRecordSetProvider {


    private TdEngineRecordSetProvider() {
    }


    private static TdEngineRecordSetProvider instance;

    public static TdEngineRecordSetProvider getInstance() {
        if (instance == null) {
            synchronized (TdEngineRecordSetProvider.class) {
                if (instance == null) {
                    instance = new TdEngineRecordSetProvider();
                }
            }
        }
        return instance;
    }

    @Override
    public RecordSet getRecordSet(ConnectorTransactionHandle transactionHandle
            , ConnectorSession session, ConnectorSplit split, List<? extends ColumnHandle> columns) {
        TdEngineSplit tdEngineSplit = (TdEngineSplit) split;

        ImmutableList.Builder<TdEngineColumnHandle> handles = ImmutableList.builder();
        for (ColumnHandle handle : columns) {
            TdEngineColumnHandle tdEngineColumnHandle = (TdEngineColumnHandle) handle;
            //log.debug("------------------------handleName:{},handleType:{}", kairosdbHandle.getColumnName(),kairosdbHandle.getColumnType());
            handles.add(tdEngineColumnHandle);
        }

        return new TdEngineRecordSet(tdEngineSplit, handles.build());
    }
}
